package fr.umlv.ig.infoView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import fr.umlv.ig.metroFactory.GraphicFactory;
import fr.umlv.ig.model.Itinerary;
import fr.umlv.ig.model.Line;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Train;

@SuppressWarnings("serial")

/**
 * this class is used for showing informations about a line
 */
public class LineInfoView extends JPanel {

	/**
	 * model use of collect information
	 */
	private MetroModelImpl metroModel;
	
	/**
	 * line that the infoview is showing
	 */
	private Line line;
	
	/**
	 * generator use for generate random itinerary
	 */
	private static final Random generator = new Random();
	

	private static final String stringLine =  new String();
	private static final String countStations =  new String("Number of Stations : ");
	private static final String countTrains=  new String("Number of trains : ");


	private final JLabel labelNumber = new JLabel(stringLine);
	private final JLabel labelNbStations = new JLabel(countStations);
	private final JLabel labelNbTrains = new JLabel(countTrains);
	private final JButton buttonRandomTrain = new JButton("Add random trains");
	private final JButton buttonConfig = new JButton("Line Configuration");

	//private ConfigurationWindow config;

	/**
	 * initialize the lineInfoView
	 * @param frame frame for create configuration window
	 * @param metroModel model use for getting informations
	 */
	public LineInfoView( final JFrame frame, final MetroModelImpl metroModel, ActionListener toolWindow) {
		super();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(Box.createVerticalStrut(10));
		add(labelNumber);
		add(Box.createVerticalStrut(7));
		add(labelNbStations);
		add(Box.createVerticalStrut(7));
		add(labelNbTrains);
		add(Box.createVerticalStrut(7));
		add(buttonRandomTrain);
		add(Box.createVerticalStrut(7));
		add(buttonConfig);
		this.metroModel = metroModel;
		//config = cw;
		buttonRandomTrain.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int random = generator.nextInt(metroModel.getCountItinerary(line));
				Itinerary itinerary = metroModel.getItinerary(line, random);
				Train train = new Train(itinerary);
				metroModel.addTrain(line, train);
			}
		});

//		buttonConfig.addActionListener(new ActionListener() {
//
//
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				if(config == null) {
//					config = new ConfigurationWindow(frame, "Line configuration", metroModel);
//					config.setSelectedTab(Integer.valueOf(line.getNumber())-1);
//				}
//				else {
//					config.setSelectedTab(Integer.valueOf(line.getNumber())-1);
//				}
//				config.start();	
//			}
//		});
		buttonConfig.addActionListener(toolWindow);
	}
	
	/**
	 * set the info
	 * @param l line that will be use by model for getting information
	 */
	public void setInfo(final Line l) {
		line = l;
		//labelNumber.setText(stringLine + l.getNumber());
		labelNumber.setIcon(GraphicFactory.getLineIcone(l.getNumber()));
		labelNbStations.setText(countStations + metroModel.getCountStation(l));
		labelNbTrains.setText(countTrains + metroModel.getCountTrain(l));
	}


}
